import {Component, Inject, OnInit, ViewChild} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from "@angular/material/dialog";
import {GnMessageService, GnTableColumn, GnTableComponent} from "geminis-ng";
import {HttpClient} from "@angular/common/http";
import {PropertyComponent} from "./property/property.component";
import {InputTypes} from "./InputTypes";
import {GnFormConfig} from "../../../api/FormConfig";

@Component({
  selector: 'gn-workflow-manager-model-form',
  templateUrl: './form.component.html'
})
export class FormComponent implements OnInit {

  editType = PropertyComponent;

  propertyColumns: GnTableColumn[] = [
    {label: '显示名称', field: 'label'},
    {label: '数据项', field: 'field'},
    {label: '顺序', field: 'showOrder'},
    {label: '跨行', field: 'rowspan'},
    {label: '跨列', field: 'colspan'},
    {label: '输入类型', field: 'type', formatter: (row) => InputTypes.find(value => value.value === row.type).text}
  ]

  @ViewChild(GnTableComponent, {static: true}) table: GnTableComponent;

  formValue: GnFormConfig = {name: null, columns: 0, properties: []};

  constructor(@Inject(MAT_DIALOG_DATA) private data: any,
              private dialogRef: MatDialogRef<any>,
              private httpClient: HttpClient,
              private messageService: GnMessageService) {
  }

  ngOnInit(): void {
    this.httpClient.get("/WORKFLOW-SERVICE/forms/" + this.data.id).subscribe((value: GnFormConfig) => {
        this.formValue = value;
        this.table.setData(this.formValue.properties.sort((p1, p2) => p1.showOrder - p2.showOrder));
      },
      error => {
        const submitError = {
          message: "获取数据错误",
          error: error
        };
        this.messageService.error(submitError);
      });
  }

  orderTable() {
    this.formValue.properties = this.formValue.properties.sort((p1, p2) => p1.showOrder - p2.showOrder);
    this.table.table.renderRows();
  }

  addProperty(value: any) {
    if (value.showOrder <= 0) {
      value.showOrder = this.formValue.properties.length + 1;
    }
    this.formValue.properties.push(value);
    this.orderTable();
  }

  save() {
    this.httpClient.put("/WORKFLOW-SERVICE/forms/", this.formValue).subscribe(() => {
        this.dialogRef.close('confirmed');
      },
      error => {
        const submitError = {
          message: "保存数据错误",
          error: error
        };
        this.messageService.error(submitError);
      });
  }


  static dialog: MatDialog;
  static httpClient: HttpClient;

  static open(id: string): void {
    FormComponent.dialog.open(FormComponent,
      {
        width: "90%",
        maxWidth: "100vw",
        height: "80%",
        maxHeight: "100vw",
        data: {id: id},
        disableClose: true
      });
  }

  // static getProperties(id: string, callback: Function): void {
  //   this.httpClient.get("/WORKFLOW-SERVICE/forms/" + id)
  //     .subscribe((value: any) => {
  //       value = value.properties.sort((p1, p2) => p1.showOrder - p2.showOrder);
  //       callback(value);
  //     })
  // }

}
